package com.example.wtt.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.wtt.controller.dto.CollectDTO;
import com.example.wtt.controller.dto.DynamicDTO;
import com.example.wtt.entity.Dynamic;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface DynamicMapper extends BaseMapper<Dynamic> {
//    @Select("SELECT u.id AS user_id, d.dynamic_id, u.username, u.avatar AS avatar_url, d.dynamic_url, d.dynamic_text, d.create_time AS dynamic_create_time, d.title, d.collect, c.text AS comment_text, c.comment_time AS comment_create_time FROM sys_user u JOIN dynamic d ON u.id = d.user_id LEFT JOIN comments c ON d.dynamic_id = c.dynamic_id")
//    List<DynamicDTO> allDynamics();

    @Select("insert into dynamic (user_id, create_time ,dynamic_url, dynamic_text, title) values (#{user_id}, #{create_time}, #{img}, #{text_content}, #{title});")
    Object publishDynamic(Object obj);

//    @Select("SELECT u.id AS user_id, d.dynamic_id, u.username, u.user_status, u.avatar AS avatar_url, d.dynamic_url, d.dynamic_text, d.create_time AS dynamic_create_time, d.title, d.collect, d.upvote, c.text AS comment_text, c.comment_time AS comment_create_time FROM sys_user u JOIN dynamic d ON u.id = d.user_id LEFT JOIN comments c ON d.dynamic_id = c.dynamic_id LIMIT #{pageNum}, #{pageSize}")
    @Select("SELECT u.id AS user_id, d.dynamic_id, u.username, u.user_status, u.avatar AS avatar_url, d.dynamic_url, d.dynamic_text, d.create_time AS dynamic_create_time, d.title, d.collect, d.upvote FROM sys_user u JOIN dynamic d ON u.id = d.user_id LIMIT #{pageNum}, #{pageSize}")
    List<DynamicDTO> allDynamics(Integer pageNum, Integer pageSize);

    @Update("UPDATE dynamic SET upvote = upvote + 1 WHERE dynamic_id = #{dynamicId};")
    Integer upvote(Integer dynamicId);

    @Update("UPDATE dynamic SET upvote = upvote + 1 WHERE dynamic_id = #{dynamicId};")
    Integer upvoteSum(Integer dynamicId);

    @Update("UPDATE dynamic SET upvote = upvote - 1 WHERE dynamic_id = #{dynamicId};")
    Integer upvoteSub(Integer dynamicId);

    @Insert("insert into dc (user_id, dynamic_id) values (#{userId}, #{dynamicId});")
    Integer collectSum(CollectDTO collectDTO);

    @Delete("DELETE FROM dc WHERE user_id = #{userId} AND dynamic_id = #{dynamicId};")
    Integer collectSub(CollectDTO collectDTO);

    @Select("SELECT IF(COUNT(*) > 0, 1, 0) AS result FROM dc WHERE user_id = #{userId} AND dynamic_id = #{dynamicId};")
    Integer queryCollect(CollectDTO collectDTO);

    @Select("SELECT u.id AS user_id, d.dynamic_id, u.username, u.user_status, u.avatar AS avatar_url, d.dynamic_url, d.dynamic_text, d.create_time AS dynamic_create_time, d.title, d.collect, d.upvote FROM sys_user u JOIN dynamic d ON u.id = d.user_id WHERE title LIKE CONCAT('%', #{describe}, '%');")
    List<DynamicDTO> queryDynamic(String describe);
}
